﻿#include "forward_control.h"
#include <QDebug>

Forward_Control::Forward_Control(QObject *parent) : QObject(parent)
{
    qRegisterMetaType<Forward_Node*>("Forward_Node");
}

void Forward_Control::openPort(QJsonObject info)
{
    qDebug()<<"开启转发: ";
    Forward_Node *node = new Forward_Node(info,this);
    qDebug()<<"连接信号: ";
    connect(this,&Forward_Control::releaseNode,node,&Forward_Node::stop,Qt::QueuedConnection);
    
    qDebug()<<"获取: ";
    int appPort = info["appPort"].toInt();
    qDebug()<<"插入: ";
    m_NodeMap.insert(appPort,node);  //记录下来
    
    node->start();
}

void Forward_Control::closePort(QJsonObject info)
{
    int appPort = info["appPort"].toInt();
    qDebug()<<"关闭转发: "<<appPort;
    
    QMultiMap<int,Forward_Node*>::iterator i = m_NodeMap.find(appPort);
    while (i != m_NodeMap.end() && i.key() == appPort) {
        emit releaseNode(i.value());  //通知释放
        ++i;
    }
}
